package com.hbnu.personaldiaryweb.mapper;

import com.hbnu.personaldiaryweb.entity.Diary;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface DiaryMapper {



    // 插入日记
    @Insert("INSERT INTO diary (id, user_id, diary_title, diary_body, date_time, label, life_state) " +
            "VALUES (#{diary.id}, #{diary.user_id}, #{diary.title}, #{diary.diary_body}, #{diary.date_time}, #{diary.label}, #{diary.life_state})")
    int insertDiary(@Param("diary") Diary diary);



    // 根据 ID 查询日记
    @Select("SELECT * FROM diary WHERE id = #{id}")
    @Results({
            @Result(property = "id", column = "id", jdbcType = JdbcType.VARCHAR),
            @Result(property = "user_id", column = "user_id", jdbcType = JdbcType.VARCHAR),
            @Result(property = "title", column = "diary_title", jdbcType = JdbcType.VARCHAR),
            @Result(property = "diary_body", column = "diary_body", jdbcType = JdbcType.VARCHAR),
            @Result(property = "date_time", column = "date_time", jdbcType = JdbcType.VARCHAR),
            @Result(property = "label", column = "label", jdbcType = JdbcType.VARCHAR),
            @Result(property = "life_state", column = "life_state", jdbcType = JdbcType.VARCHAR)
    })
    Diary getDiaryById(@Param("id") String id);




    // 查询所有日记
    @Select("SELECT id, user_id, diary_title, diary_body, date_time, label, life_state FROM diary")
    @Results({
            @Result(property = "id", column = "id", jdbcType = JdbcType.VARCHAR),
            @Result(property = "user_id", column = "user_id", jdbcType = JdbcType.VARCHAR),
            @Result(property = "title", column = "title", jdbcType = JdbcType.VARCHAR),
            @Result(property = "diary_body", column = "diary_body", jdbcType = JdbcType.VARCHAR),
            @Result(property = "date_time", column = "date_time", jdbcType = JdbcType.VARCHAR),
            @Result(property = "label", column = "label", jdbcType = JdbcType.VARCHAR),
            @Result(property = "life_state", column = "life_state", jdbcType = JdbcType.VARCHAR)
    })
    List<Diary> getAllDiaries();



    // 更新日记信息
    @Update("UPDATE diary SET user_id = #{diary.user_id}, diary_title = #{diary.title}, diary_body = #{diary.diary_body}, " +
            "date_time = #{diary.date_time}, label = #{diary.label}, life_state = #{diary.life_state} WHERE id = #{diary.id}")
    int updateDiary(@Param("diary")Diary diary);



    // 根据 ID 删除日记
    @Delete("DELETE FROM diary WHERE id = #{id}")
    int deleteDiaryById(@Param("id")String id);



    // 根据用户 ID 查询日记
    @Select("SELECT id, user_id, diary_title, diary_body, date_time, label, life_state FROM diary WHERE user_id = #{user_id}")
    @Results({
            @Result(property = "id", column = "id", jdbcType = JdbcType.VARCHAR),
            @Result(property = "user_id", column = "user_id", jdbcType = JdbcType.VARCHAR),
            @Result(property = "title", column = "title", jdbcType = JdbcType.VARCHAR),
            @Result(property = "diary_body", column = "diary_body", jdbcType = JdbcType.VARCHAR),
            @Result(property = "date_time", column = "date_time", jdbcType = JdbcType.VARCHAR),
            @Result(property = "label", column = "label", jdbcType = JdbcType.VARCHAR),
            @Result(property = "life_state", column = "life_state", jdbcType = JdbcType.VARCHAR)
    })
    List<Diary> getDiariesByUserId(@Param("user_id")String user_id);



    //根据分类查看日记，输入一个分类的label，输出所有该用户的日记
    //包括所有属性
    @Select("SELECT id, user_id, diary_title, diary_body, date_time, label, life_state FROM diary WHERE user_id = #{user_id} and label = #{label}")
    @Results({
            @Result(property = "id", column = "id", jdbcType = JdbcType.VARCHAR),
            @Result(property = "user_id", column = "user_id", jdbcType = JdbcType.VARCHAR),
            @Result(property = "title", column = "title", jdbcType = JdbcType.VARCHAR),
            @Result(property = "diary_body", column = "diary_body", jdbcType = JdbcType.VARCHAR),
            @Result(property = "date_time", column = "date_time", jdbcType = JdbcType.VARCHAR),
            @Result(property = "label", column = "label", jdbcType = JdbcType.VARCHAR),
            @Result(property = "life_state", column = "life_state", jdbcType = JdbcType.VARCHAR)
    })
    List<Diary> getDiariesAllByUserAndLabel(@Param("user_id")String user_id,@Param("label")String label);


    //根据用户和标签查找
    //查看缩略属性
    @Select("SELECT id, diary_title, date_time FROM diary WHERE user_id = #{user_id} and label = #{label}")
    @Results({
            @Result(property = "id", column = "id", jdbcType = JdbcType.VARCHAR),
            @Result(property = "title", column = "diary_title", jdbcType = JdbcType.VARCHAR),
            @Result(property = "date_time", column = "date_time", jdbcType = JdbcType.VARCHAR),
    })
    List<Diary> getDiariesMiniByUserAndLabel(@Param("user_id")String user_id,@Param("label")String label);


    //查看所有该用户的label标签，不重复
    @Select("SELECT DISTINCT label FROM diary WHERE user_id = #{user_id}")
    List<String> getLabelsByUser(@Param("user_id") String user_id);

}
